//
//  WTRecommendListCell.m
//  OneDay
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTRecommendListCell.h"
#import <UIImageView+WebCache.h>

// 距离边框的长度
#define kMargin (20 *  kRelativeLength_X)
// 图片的长度
#define kPicOneWidth (120 *  kRelativeLength_X)
// 图片的高度
#define kPicOneHeight (80 *  kRelativelength_Y)

// 图片距离标题的
#define kPicToTitle 5 * kRelativelength_Y
// 热门,推荐这个图片的长度
#define kMarkImageWidth 15 * kRelativeLength_X

// TimeAgoLabel的长度
#define kTimeAgoLabelWidth 100 * kRelativeLength_X

// commentLabel的长度
#define kCommentLabelWidth 100 * kRelativeLength_X

@implementation WTRecommendListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self addSubviews];
    }
    return self;
}

// 添加子控件
- (void)addSubviews
{
    // 图片
    _picOneImageV = [[UIImageView alloc]initWithFrame:CGRectMake(kMargin, kMargin, kPicOneWidth, kPicOneHeight)];
    _picOneImageV.image = [UIImage imageNamed:@"m-zanweitu"];
    [self.contentView addSubview:_picOneImageV];
    
    // 标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_picOneImageV.right + kPicToTitle, _picOneImageV.top, kScreenWidth - _picOneImageV.right - kPicToTitle, kPicOneHeight / 3 * 2)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView  addSubview:_titleLabel];
    
    
    // 推荐或者热点状态
    _markImagView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.left, _picOneImageV.bottom - kMarkImageWidth, kMarkImageWidth, kMarkImageWidth)];
    [self.contentView  addSubview:_markImagView];
    
    // 多长时间前
    _timeAgoLabel = [[UILabel alloc]initWithFrame:CGRectMake(_markImagView.right + kPicToTitle * 2, _markImagView.top, kTimeAgoLabelWidth, _markImagView.height)];
    _timeAgoLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView  addSubview:_timeAgoLabel];
    
    
    // 评论
    _commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - kCommentLabelWidth - kMargin, _timeAgoLabel.top, kCommentLabelWidth, _timeAgoLabel.height)];
    _commentLabel.font = [UIFont systemFontOfSize:14];
    _commentLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView  addSubview:_commentLabel];
    
    // 下面的那条分割线
    UIView *  bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _commentLabel.bottom + _timeAgoLabel.height - kRelativelength_Y, kScreenWidth, kRelativelength_Y)];
    bottomView.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:bottomView];
}


- (void)setModel:(WTRecommendListModel *)model
{
    _model = model;
    [_picOneImageV sd_setImageWithURL:[NSURL URLWithString:model.picOne]placeholderImage:[UIImage imageNamed:@"m-aznweitu"]];
   
    if ([model.mark isEqualToString:@"1"]) {
        _markImagView.image = [UIImage imageNamed:@"m-tuijian"];
        _markImagView.frame = CGRectMake(_titleLabel.left, _picOneImageV.bottom - kMarkImageWidth, kMarkImageWidth, kMarkImageWidth);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right + kPicToTitle * 2, _markImagView.top, kTimeAgoLabelWidth, _markImagView.height);
        
    } else if ([model.mark isEqualToString:@"2"]){
        _markImagView.image = [UIImage imageNamed:@"m-remen"];
        _markImagView.frame = CGRectMake(_titleLabel.left, _picOneImageV.bottom - kMarkImageWidth, kMarkImageWidth, kMarkImageWidth);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right + kPicToTitle * 2, _markImagView.top, kTimeAgoLabelWidth, _markImagView.height);
    }else {
        _markImagView.frame = CGRectMake(_markImagView.left, _markImagView.top, 0, _markImagView.height);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right, _markImagView.top, kTimeAgoLabelWidth, _markImagView.height);
    }
    
    _titleLabel.text = model.title;
    _timeAgoLabel.text = model.timeAgo;
    
    
    _commentLabel.text = [NSString stringWithFormat:@"%@评论",model.commentNum];
}

// 返回cell高度的方法
+ (CGFloat)heightForRow:(WTRecommendListModel *)model
{

    return kMargin + kPicOneHeight + kMargin;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
